package com.hbt.orderfood.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.utils.StringUtils;

import java.util.LinkedList;
import java.util.List;

public class UserOrderAdapter extends RecyclerView.Adapter {
    private List<Order> orders = StringUtils.orders;
    private DBAdapter dbAdapter;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(dbAdapter==null){
            dbAdapter = new DBAdapter(parent.getContext());
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layout = new LinearLayout(parent.getContext());
        layout.setLayoutParams(params);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_order_item,layout,true);
        UserOrderAdapter.ViewHolder holder = new UserOrderAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder hold = (ViewHolder)holder;
        Order order = orders.get(position);
        Food food = dbAdapter.getFoodByFoodID(String.valueOf(order.getFood_id()));
        hold.tv_name.setText(food.getName());
        hold.tv_price.setText("价格:"+String.valueOf(order.getPrice())+"￥");
        hold.tv_num.setText(String.valueOf(order.getNum()));
        StringUtils.setImg(hold.img,food.getType());
        hold.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.clickAddBtn(food);
                notifyDataSetChanged();
            }
        });
        hold.sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.clickSubBtn(food);
                notifyDataSetChanged();
            }
        });
    }//onBindViewHolder

    @Override
    public int getItemCount() {
        return orders.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_name;
        TextView tv_price;
        TextView tv_num;
        Button sub;
        Button add;
        ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.name);
            tv_price = itemView.findViewById(R.id.price);
            tv_num = itemView.findViewById(R.id.num);
            sub = itemView.findViewById(R.id.sub);
            add = itemView.findViewById(R.id.add);
            img = itemView.findViewById(R.id.img);
        }
    }
    public interface OnClickAddSubListener{
        void clickAddBtn(Food food);
        void clickSubBtn(Food food);
    }

    public void setListener(OnClickAddSubListener listener) {
        this.listener = listener;
    }

    private OnClickAddSubListener listener;
}
